/**
 * 
 */
package eu.news.data.hibernate.dao;

import java.util.List;

import org.hibernate.Query;

import eu.news.data.dao.SortDAO;
import eu.news.data.hibernate.pojo.HB_Sort;
import eu.news.data.pojo.Sort;
import eu.news.data.pojo.Special;


/**
 * @author WZZ
 *
 */
public class HB_SortDAO extends HibernateBaseDAO<Sort> implements SortDAO {
	
	protected HB_SortDAO(){}
	
	@SuppressWarnings("unchecked")
	public List<Sort> getByName(String name){
		String hql="from HB_Sort where name=:name";
		Query query=session.createQuery(hql);
		query.setString("name", name);
		return query.list();
	}
	
	public Sort getById(int id){
		String hql="from HB_Sort where id=:id";
		Query query=session.createQuery(hql);
		query.setInteger("id", id);
		return (Sort) query.uniqueResult();
	}
	
	public Sort getByNameSpecial(String name,Special sp){
		String hql="from HB_Sort where name=:name and special_id=:sp";
		Query query=session.createQuery(hql);
		query.setString("name", name);
		query.setInteger("sp", sp.getId());
		return (Sort) query.uniqueResult();
	}
	
	@SuppressWarnings("unchecked")
	public List<Sort> getBySpecial(Special sp){
		if(sp==null)
			return null;
		String hql="from HB_Sort where special_id=:sp";
		Query query=session.createQuery(hql);
		query.setInteger("sp", sp.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Sort> getBySpecialName(String name,int start,int size){
		String hql="from HB_Sort where special.name=:name";
		Query query=session.createQuery(hql);
		query.setString("name", name);
		query.setFirstResult(start);
		query.setMaxResults(size);
		return query.list();
	}

	@Override
	public Sort newEntity() {
		return new HB_Sort();
	}
}
